package com.henry.javastudy.c04;


import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class C04Application {

    public static void main(String[] args) {
        //m0(); //使用thread.run+自定义Runnable
        //m1(); //使用thread.start + thread.join+自定义Runnable
        //m2(); //使用CountDownLatch+自定义Runnable
        //m3();// 使用FutureTask<V> +Callable实现
        //m4();// 使用Future<V> +Callable实现
        //m5(); //使用synchronized锁住同一个对象
        m8();//使用while(thread.isAlive)判断
    }

    //同步
    private static void m0(){
        long start=System.currentTimeMillis();
        MyRunnable task =new MyRunnable();
        Thread thread =new Thread(task);
        thread.run();
        try {
            System.out.println("异步计算结果为：" + task.getResult());
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        }catch (Exception e){

        }

    }


    private static void m1(){
        long start=System.currentTimeMillis();
        try {
            MyRunnable task=new MyRunnable();
            Thread thread =new Thread(task);
            thread.setDaemon(true);
            thread.start();
            thread.join(); //等待子线程结束
            int result =task.getResult();
            System.out.println("异步计算结果为：" + result);

            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    private static void m2(){
        long start=System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        MyRunnable1 task=new MyRunnable1(countDownLatch);
        new Thread(task).start();

        try {
            countDownLatch.await();
            int result = task.getResult();
            System.out.println("异步计算结果为：" + result);
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    private static void m3(){
        long start=System.currentTimeMillis();

        // 在这里创建一个线程或线程池，
        // 异步执行 下面方法
        FutureTask<Integer> futureTask =new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return MyCanculate.sum();
            }
        });
        Thread thread =new Thread(futureTask);
        thread.setDaemon(true);
        thread.start();
        try {
            //这是得到的返回值
            int result = futureTask.get();

            // 确保  拿到result 并输出
            System.out.println("异步计算结果为：" + result);

            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    private static void m4(){

        long start=System.currentTimeMillis();
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Integer> result = executor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return MyCanculate.sum();
            }
        });
        executor.shutdown();
        try {
            System.out.println("异步计算结果为：" + result.get());
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    private static void m5(){
        Object lockObj =new Object();
        long start=System.currentTimeMillis();
        MyRunnable2 task =new MyRunnable2(lockObj);
        Thread thread =new Thread(task);
        thread.setDaemon(true);
        thread.start();
        try {
            synchronized (lockObj) {
                lockObj.wait();
                System.out.println("异步计算结果为：" + task.getResult());
                System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
            }
        }catch (Exception e){

        }

    }
    private static void m6(){
        ReentrantLock lock = new ReentrantLock();
        long start=System.currentTimeMillis();
        MyRunnable4 task =new MyRunnable4(lock);
        Thread thread =new Thread(task);
        thread.setDaemon(true);
        thread.run();
        try {
            lock.lock();
            System.out.println("异步计算结果为：" + task.getResult());
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        }catch (Exception e) {

        }
        finally {
            lock.unlock();
        }
    }

    private static void m7(){
        Semaphore semaphore =new Semaphore(1);
        long start=System.currentTimeMillis();
        MyRunnable3 task =new MyRunnable3(semaphore);
        Thread thread =new Thread(task);
        thread.start();
        try {
            semaphore.acquire();
            System.out.println("异步计算结果为：" + task.getResult());
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
            semaphore.release();

        }catch (Exception e) {

        }
    }
    private static void m8(){
        long start=System.currentTimeMillis();
        MyRunnable task =new MyRunnable();
        Thread thread =new Thread(task);
        thread.start();
        try {
            while(thread.isAlive()){
                Thread.sleep(10);
            }
            System.out.println("异步计算结果为：" + task.getResult());
            System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        }catch (Exception e){

        }

    }
}
